package m.jclecai.com.fragment.follow;


import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.AdapterFragment;
import m.jclecai.com.base.BaseLasyLoadFragment;
import m.jclecai.com.bean.Response.ResHotPeopleInfo;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GenDanHongRenDetailFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GenDanHongRenDetailFragment extends BaseLasyLoadFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ResHotPeopleInfo mResHotPeopleInfo = new ResHotPeopleInfo();
    private String mHotPeopleId;
//    private HotPeopleInfoAdapter mHotPeopleInfoAdapter;
//    @BindView(R.id.prl_list_view)
//    PullToRefreshListView mRefreshListView;

    @BindView(R.id.rv_gendan)
    RecyclerView rv_gendan;
    //    ListView mListView;
    private AdapterFragment adapterFragment;
    private int type;

    public GenDanHongRenDetailFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @return A new instance of fragment GenDanHongRenDetailFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static GenDanHongRenDetailFragment newInstance(int param1, String param2) {
        GenDanHongRenDetailFragment fragment = new GenDanHongRenDetailFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void lazyLoad() {
        if (mHotPeopleId == null) {
            mHotPeopleId = getArguments().getString("param2");
//        mListView = mRefreshListView.getRefreshableView();
//        WrappingGridLayoutManager gridLayoutManager = new WrappingGridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false);
//        rv_gendan.setLayoutManager(gridLayoutManager);
            rv_gendan.setLayoutManager(new LinearLayoutManager(getActivity()));
            //消除滑动卡顿现象 ...
            rv_gendan.setNestedScrollingEnabled(false);//限制recyclerview自身滑动特性,滑动全部靠scrollview完成
            adapterFragment = new AdapterFragment(getActivity(), mResHotPeopleInfo.getRecords());
            rv_gendan.setAdapter(adapterFragment);
            rv_gendan.setHasFixedSize(true);//固定自身size不受adapter变化影响
            Log.v("fragment_load", "load" + type);
            requestHotOrder(mHotPeopleId);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_gen_dan_hong_ren_detail;
    }

    private void requestHotOrder(String hotPeopleId) {
        //这里我们要请求的是热门的跟单；
        showDialog();
        type = getArguments().getInt(ARG_PARAM1, 0);
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("id", hotPeopleId);
        paramsMap.put("classid", type + "");
//        showDialog();
        HttpManager.postAsyn(Constant_Url.FOLLOW_HOT_ORDER, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                ((HotPeopleInfoActivity) getActivity()).scrollView_main.scrollTo(0, 0);
                DisPlay("网络异常!");
                dissDialog();
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                try {
                    if (!TextUtils.isEmpty(response)) {
                        mResHotPeopleInfo = FastOk.get(response, ResHotPeopleInfo.class);
                        if (mResHotPeopleInfo != null) {
                            if ("0".equals(mResHotPeopleInfo.getStatus())) {
                                adapterFragment = new AdapterFragment(getActivity(), mResHotPeopleInfo.getRecords());
                                rv_gendan.setAdapter(adapterFragment);
//                                adapterFragment.refresh(mResHotPeopleInfo.getRecords());
                                rv_gendan.setHasFixedSize(true);//固定自身size不受adapter变化影响
                            } else {
                                DisPlay(mResHotPeopleInfo.getMessage());
                            }

                        } else {
                            DisPlay("网络异常!");
                        }
                    } else {
                        DisPlay("网络异常!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    DisPlay("网络异常!");
                }

            }
        }, paramsMap);

    }

    public void loadMore() {
        requestHotOrder(mHotPeopleId);
    }
}
